package sk.bielyvlk.vlkgps.osd;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;

public class OsdButton extends OsdWidget {
	private float left;
	private float top;
	private BitmapDrawable bitmap;
	private boolean down;
	private OsdButtonEvent btnEvent;
	
	public OsdButton(Context context, OsdButtonEvent btnEvent, float x, float y, int id) {
		this.btnEvent = btnEvent;
		left = x;
		top = y;
		bitmap = (BitmapDrawable)context.getResources().getDrawable(id);
	}
	
	public OsdButton(Context context, OsdButtonEvent btnEvent, int id) {
		this.btnEvent = btnEvent;
		bitmap = (BitmapDrawable)context.getResources().getDrawable(id);
	}
	
	public void setPosition(float x, float y) {
		left = x;
		top = y;
	}
	
	public void onDraw(Canvas canvas, Paint paint) {
		canvas.drawBitmap(bitmap.getBitmap(), left, top, paint);
	}
	
	public boolean onEvent(MotionEvent event) {
		if (btnEvent == null) return false;
		Bitmap bmp = bitmap.getBitmap();
		if ((event.getX() > left) && (event.getY() > top) && (event.getX() < left + bmp.getWidth()) && (event.getY() < top + bmp.getHeight())) {
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN :
					down = true;
					btnEvent.onButtonEvent(this, OsdButtonEvent.DOWN);
					break;
				case MotionEvent.ACTION_UP :
					down = false;
					btnEvent.onButtonEvent(this, OsdButtonEvent.UP);
					break;
			}
			return true;
		}
		if (down) {
			switch (event.getAction()) {
				case MotionEvent.ACTION_UP :
					down = false;
					break;
			}
		}
		return false;
	}
}
